package com.bprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bprogram.controller.UserFeign;
import com.bprogram.controller.VideoFeign;
import com.bprogram.mapper.HistoryMapper;
import com.bprogram.pojo.Comment;
import com.bprogram.pojo.History;
import com.bprogram.pojo.User;
import com.bprogram.service.HistoryService;

import com.bprogram.utils.IdGenerator;
import com.bprogram.utils.RedisUtil;
import org.springframework.stereotype.Service;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cai
 * @since 2023-11-21
 */
@Service
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements HistoryService {


    @Resource
    private HistoryMapper historyMapper;
    @Resource
    private UserFeign userFeign;

    @Resource
    private VideoFeign videoFeign;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public IPage<History> getHistoryListCondition(String currentPage, String pageSize) {
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        User loginUser = userFeign.getLoginUser();
        if (StringUtils.isEmpty(loginUser.getUserId())) {
            wrapper.eq("createId", loginUser.getUserId());
        }
        IPage<History> page = new Page<>();
        long ps = Long.parseLong(pageSize);
        long cp = Long.parseLong(currentPage);
        page.setSize(ps);
        page.setCurrent(cp);
        IPage<History> historyIPage = historyMapper.selectPage(page, wrapper);
        List<History> historyList = historyIPage.getRecords();
//流的形式给一个list里面的某个属性赋值
        historyList.stream().forEach(history -> {
            history.setVideo(videoFeign.getVideo(history.getVideoId()));
            history.setUser(userFeign.getById(history.getUpId()));
        });
        //将historyList以流的形式取其id并存到一个list中
//        List<Long> idList = historyList.stream().map(History::getHistoryId).collect(Collectors.toList());

        historyIPage.setRecords(historyList);
        return historyIPage;

    }

    @Override
    public boolean saveOrUpdateHistory(History history) {
        String userId = userFeign.getLoginUser().getUserId()+"";

        String videoId = history.getVideoId()+"";
        long l = redisUtil.sSet(userId, videoId);
if (l>0){
    if (StringUtils.isEmpty(history.getHistoryId())) {
        IdGenerator idGenerator = new IdGenerator();
        history.setHistoryId(idGenerator.snowflakeId());
        return this.save(history);
    }
}

        History updateHistory = new History();
        UpdateWrapper<History> wrapper = new UpdateWrapper<>();


        wrapper.eq("historyId", history.getHistoryId());
        return this.update(updateHistory, wrapper);
    }




}
